package wh.coach._lambda;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author hangwen
 * @date 2023/7/1
 */
public class _Function {

    public static void main(String[] args) {
        callRunnable(() -> System.out.println("hello world"));

        callConsume(str -> System.out.println("hello " + str), "pig");

        int result = callFunction(str -> str.length(), "hello");
        //也可以这样写
        result = callFunction(String::length, "hello");
        System.out.println("hello.length is:" + result);

        final boolean isEmpty = callPredicate(str -> str.isEmpty(), "");
        System.out.println("empty:" + isEmpty);

        Supplier<String> function = () -> "abc";
        final String s = function.get();
    }

    private static void callConsume(final Consumer<String> consumer, final String str) {
        consumer.accept(str);
    }

    private static int callFunction(final Function<String, Integer> function, final String str) {
        return function.apply(str);
    }

    private static boolean callPredicate(final Predicate<String> predicate, final String str) {
        return predicate.test(str);
    }

    private static void callRunnable(final Runnable runnable) {
        runnable.run();
    }

}
